package com.pss.common.util;

import com.alibaba.fastjson.JSON;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class JsonString {

	public static void writeJsonString(HttpServletResponse response, Object obj) {
		if (obj == null) {
			throw new NullPointerException("obj is null");
		}
		try {
			String json = JSON.toJSONString(obj);
			if (json.startsWith("[")) {
				json = json.substring(1);
			}
			if (json.endsWith("]")) {
				json = json.substring(0, json.length() - 1);
			}
			response.setCharacterEncoding("utf-8");
			response.getWriter().write(json);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
